import isCode from 'plus/inputCode'
import Verify from 'plus/getVerify'

const mixins ={
   mixin:{
    data() {
      const validatePhoneUser = (rule, value, callback) => {
        let result = isCode.isPhone(value)
        switch (result.code){
          case 1:
            callback(new Error(result.msg))
            break
          case 2:
            callback(new Error(result.msg))
            break
          default:
            if(this.loginMsg !== '' && this.loginMsg !== undefined){
              callback(new Error(this.loginMsg))
              this.loginMsg = ''
            }else {
              callback()
              this.checkPhone = true
              this.ableLogin = false
              this.ableCode = false
            }
            break
        }
      }
      const validateCodeUser = (rule, value, callback) => {
        if(value.length < 6 && this.loginMsg == ''){
          callback(new Error("请填写6位验证码"))
        }else if(this.loginMsg !== '' && this.loginMsg !== undefined){
          callback(new Error(this.loginMsg))
          this.loginMsg = ''
        }else {
          callback()
        }
      }
      const validateCodeUserPic = (rule, value, callback) => {
        let x = rule.field
        if(value.length < 5){
          callback(new Error("请填写5位验证码"))
        }else if(value.length == 5 ){
          if(this[x].validate(value)){
            callback()
          }else {
            callback(new Error("请输入正确验证码"))
          }
        }
      }
      const validateUserName = (rule, value, callback) => {
        let result = isCode.isPhone(value)
        switch (result.code){
          case 1:
            callback(new Error(result.msg))
            break
          case 2:
            callback(new Error(result.msg))
            break
          default:
            if(this.loginMsg !== ''){
              callback(new Error(this.loginMsg))
              this.loginMsg = ''
            }else {
              callback()
              this.ableLogin = false

            }
            break
        }
      }
      const validatePassword = (rule, value, callback) => {
        let result = isCode.verifyAccount(value)
        if(result.code == '1'){
          callback(new Error(result.msg))
          this.loginMsg = ''
        }else {
          if(this.loginMsg !== ''){
            callback(new Error(this.loginMsg))
            this.loginMsg = ''
          }else {
            callback()
          }
        }
      }
      return {
        loginInfo:{
          phoneUser:'',
          codeUser:'',
          codeUser1:'',
          codeUser2:'',
          userName:'',
          passWord:'',
          codePic:'',
          passWordNew:''
        },
        loginRules:{
          phoneUser:[{ required: true, trigger: 'blur', validator: validatePhoneUser }],
          codeUser:[{ required: true, trigger: 'blur', validator: validateCodeUser }],
          codeUser1:[{ required: true, trigger: 'blur', validator: validateCodeUserPic }],
          codeUser2:[{ required: true, trigger: 'blur', validator: validateCodeUserPic }],
          userName: [{ required: true, trigger: 'blur', validator: validateUserName }],
          passWord: [{ required: true, trigger: 'blur', validator: validatePassword }],
          passWordNew: [{ required: true, trigger: 'blur', validator: validatePassword }]
        },
        activeName:'loginValidateForm',
        phone:'',
        msgcode:'',
        psd:'',
        val:false,
        checkPhone:false,
        loginMsg:''
      };
    },
     methods:{
       dialogShow(formName) {
         this.loginMsg = ''
         this.$refs[formName].validate((valid) => {
           if (valid) {
            this.finishBool = true
            this.loadBool = true
            this.dialogShow1()
           } else {
            this.loadBool = false
            return false;
           }
         });
       },
       filterHtmlTagFn (msg) {
        let msgText = msg.replace(/<\/?[^>]*>/g, ''); //去除HTML Tag
            msgText = msgText.replace(/[|]*\n/, '') //去除行尾空格
            msgText = msgText.replace(/&npsp;/ig, ''); //去掉npsp
            return msgText;
      }
     }
  },
  mixinPwd:{
     data(){
       return {
         ss:''
       }

     },
     watch:{
       ss(e){
          //
       }
     }
  },
  mixinFormatTime: {
    methods: {
      formatTime(time,type){
        if(type == 1) return this.$moment(time).format('Y/M/D HH:mm')
        else if(type == 2) return this.$moment(time).format('HH:mm')
        return this.$moment(time).format('Y/M/D')
      }
    }
  },
}
export default mixins
